﻿/*
Copyright (C) 2010 INOUE Hiroyuki

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;

namespace CodePlex.Dombly.DiskUsage.ConsoleApplication
{
    #region IFileSizeInfo
    /// <summary>
    /// Defines information for reporting file size.
    /// </summary>
    interface IFileSizeInfo : IComparable<IFileSizeInfo>
    {
        string FullName { get; }
        long FileSize { get; }
    }
    #endregion

    #region FileSizeInfo
    /// <summary>
    /// Provides information for reporting file size, so that 
    /// needless fields are removed from BusinessLogic.FileSystem class.
    /// </summary>
    class FileSizeInfo : IFileSizeInfo
    {
        private readonly string _FullName;
        private readonly long _FileSize;
        public string FullName { get { return this._FullName; } }
        public long FileSize { get { return this._FileSize; } }
        public FileSizeInfo(BusinessLogic.IFileSystem source)
        {
            this._FullName = source.FullName;
            this._FileSize = source.FileSize;
            if (Options.Current.UseClusterSize)
            {
                var file = source as BusinessLogic.File;
                if (file != null && file.IsCompressed) this._FullName += " *";
            }
        }
        /// <summary>
        /// Compare in order by file size descendant, then by full name ascendant.
        /// </summary>
        public int CompareTo(IFileSizeInfo other)
        {
            int comparison = other.FileSize.CompareTo(this.FileSize);
            if (comparison == 0) comparison = this.FullName.CompareTo(other.FullName);
            return comparison;
        }
    }
    #endregion

    #region DirectorySizeInfo
    /// <summary>
    /// Provides information for reporting file size under directories,
    /// so that needless fields are removed from BusinessLogic.Directory 
    /// class.
    /// </summary>
    class DirectorySizeInfo : FileSizeInfo
    {
        private readonly int _Depth;
        private readonly FixedSizeSortedSet<IFileSizeInfo> _Directories;
        public DirectorySizeInfo(BusinessLogic.Directory source, int size)
            : base(source)
        {
            this._Depth = source.Depth;
            this._Directories = new FixedSizeSortedSet<IFileSizeInfo>(size);
            foreach (var d in source.Directories)
                this._Directories.Add(new FileSizeInfo(d));
        }
        public int Depth { get { return this._Depth; } }
        public IList<IFileSizeInfo> Directories
        { get { return this._Directories.ToList(); } }
    }
    #endregion

}
